package mybatis.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mybatis.entity.Address;
import mybatis.entity.User;
import mybatis.service.AddressService;
import mybatis.service.impl.AddressServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;


/**
 * Servlet implementation class TypeServlet
 */
@WebServlet("/address/*")
public class AddressServlet extends BaseServlet {
	private AddressService service;
	{
		service = applicationContext.getBean("addressServiceImpl",AddressServiceImpl.class);
	}
	public void getProvince(HttpServletRequest request, HttpServletResponse response) {
		Map<String,String> map = service.getProvcine();
		System.out.println(map);
		try {
			responseJson(response,map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void getCity(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		Map<String,String> map = service.getCity(id);
		System.out.println(map);
		try {
			responseJson(response,map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void getArea(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String pid = request.getParameter("pid");
		Map<String,String> map = service.getArea(pid,id);
		System.out.println(map);
		try {
			responseJson(response,map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void getTown(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String pid = request.getParameter("pid");
		String cid = request.getParameter("cid");
		Map<String,String> map = service.getTown(pid,cid,id);
		System.out.println(map);
		try {
			responseJson(response,map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void add(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		User user =(User) request.getSession().getAttribute("user");


		Map<String, String[]> parameterMap = request.getParameterMap();
		Address addr = new Address();
		addr.setUserid(user.getId());
		BeanUtils.populate(addr, parameterMap);
		System.out.println(addr);
		service.add(addr);
		findByUid(request, response);
	}
	public void findByUid(HttpServletRequest request, HttpServletResponse response) {
		User user =(User) request.getSession().getAttribute("user");
		List<Address> list = service.findByUid(user.getId());
		System.out.println(list);
		try {
			responseJson(response,list);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
